from app.app import db


class User(db.Model):
    # 指定表名
    __tablename__ = 'user'
    # 主键
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    password = db.Column(db.String(255))
    admin = db.Column(db.Integer, default=0)

    def __repr__(self):
        return 'User: %s, %s, %s, %s' % (self.id, self.name, self.password, str(self.admin))

    def to_dict(self):
        """将User对象转换为字典"""
        return {
            'id': self.id,
            'name': self.name,
            'password': self.password,
            'admin': self.admin
        }


class Book(db.Model):
    # 指定表名
    __tablename__ = 'book'
    # 主键
    book_id = db.Column(db.Integer, primary_key=True)
    desc = db.Column(db.String(10000))
    book_name = db.Column(db.String(255))
    book_author = db.Column(db.String(255))
    price = db.Column(db.String(10))
    view_count = db.Column(db.Integer, default=0)
    top = db.Column(db.Integer, default=0)
    photo = db.Column(db.String(1000))
    derive = db.Column(db.String(255))

    def __repr__(self):
        return ('Book: %s, %s, %s, %s, %s, %s, %s, %s'
                % (self.book_id, self.book_name, self.book_author, self.price, self.view_count, self.top,
                   self.desc, self.photo))

    def to_dict(self):
        """将User对象转换为字典"""
        return {
            'book_id': self.book_id,
            'book_name': self.book_name,
            'book_author': self.book_author,
            'price': self.price,
            'view_count': self.view_count,
            'top': self.top,
            'photo': self.photo,
            'derive': self.derive
        }


class Comment(db.Model):
    # 指定表名
    __tablename__ = 'comment'
    # 主键
    id = db.Column(db.Integer, primary_key=True)
    book_id = db.Column(db.Integer)
    user_id = db.Column(db.Integer)
    comment = db.Column(db.String)

    def __repr__(self):
        return ('Comment: %s, %s, %s'
                % (self.book_id, self.comment, self.user_id))
    def to_dict(self):
        """将Item对象转换为字典"""
        return {
            'id': self.id,
            'book_id': self.book_id,
            'user_id': self.user_id,
            'comment': self.comment
        }


class Item(db.Model):
    # 指定表名
    __tablename__ = 'item'
    # 主键
    item_id = db.Column(db.Integer, primary_key=True)
    book_id = db.Column(db.Integer)
    user_id = db.Column(db.Integer)
    is_pay = db.Column(db.Integer, default=0)
    is_cancel = db.Column(db.Integer, default=0)
    number = db.Column(db.Integer)

    def __repr__(self):
        return ('item: %s, %s, %s, %s, %s, %s'
                % (self.item_id, self.book_id, self.user_id, self.is_pay, self.is_cancel, self.number))

    def to_dict(self):
        """将Item对象转换为字典"""
        return {
            'item_id': self.item_id,
            'book_id': self.book_id,
            'user_id': self.user_id,
            'is_pay': self.is_pay,
            'is_cancel': self.is_cancel,
            'number': self.number
        }
